import { defineStore } from "pinia";

import {
    getCountApi,
    getKindApi,
    getGroupApi,
    getSexApi,
    getDepartmentApi,
    getIllAreaApi,
    getDateDepartmentApi,
    getTemplateApi,
    getLongTimeApi
} from '@/Api/Analysis'

import dayjs from "dayjs";
import { useCommon } from "./Common";
const CommonStore = useCommon()

export const useAnalysis = defineStore('Analysis', {
    state: () => {
        return {
            searchForm: {
                startData: dayjs().subtract(3, 'day').format('YYYY-MM-DD'),
                endData: dayjs().endOf('day').format('YYYY-MM-DD') + ' 23:59:59',
                department: '' as string | null,
                illArea: '' as string | null,
                serial: '' as string | null,
                TimeArr: [dayjs().subtract(3, 'day').format('YYYY-MM-DD'), dayjs().endOf('day').format('YYYY-MM-DD')],
                departmentWardDevice: [] as (string | null)[],
                longTime: 120,
            }
        }
    },
    actions: {
        getData() {
            if (this.searchForm.TimeArr.length) {
                this.searchForm.startData = this.searchForm.TimeArr[0]
                this.searchForm.endData = `${this.searchForm.TimeArr[1]} 23:59:59`
            }

            if (this.searchForm.departmentWardDevice && this.searchForm.departmentWardDevice.length == 3) {
                this.searchForm.department = this.searchForm.departmentWardDevice[0]
                this.searchForm.illArea = this.searchForm.departmentWardDevice[1]
                this.searchForm.serial = this.searchForm.departmentWardDevice[2]
            } else if (this.searchForm.departmentWardDevice && this.searchForm.departmentWardDevice.length == 2) {
                this.searchForm.department = this.searchForm.departmentWardDevice[0]
                this.searchForm.illArea = this.searchForm.departmentWardDevice[1]
                this.searchForm.serial = ''
            } else if (this.searchForm.departmentWardDevice && this.searchForm.departmentWardDevice.length == 1) {
                this.searchForm.department = this.searchForm.departmentWardDevice[0]
                this.searchForm.illArea = ''
                this.searchForm.serial = ''
            } else if (CommonStore.department && CommonStore.illArea) {
                this.searchForm.departmentWardDevice = [CommonStore.department, CommonStore.illArea]
                this.searchForm.department = CommonStore.department
                this.searchForm.illArea = CommonStore.illArea
            } else {
                this.searchForm.departmentWardDevice = [CommonStore.department]
                this.searchForm.department = CommonStore.department
            }


            // console.log(this.searchForm.department, CommonStore.department);

            // return Promise.all([
            //     this.getCount(),
            //     this.getKind(),
            //     this.getGroup(),
            //     this.getSex(),
            //     this.getDepartment(),
            //     this.getIllArea(),
            //     this.getDateDepartment()
            // ])
            // return Promise.all([
            //     getCountApi(this.searchForm),
            //     getKindApi(this.searchForm),
            //     getGroupApi(this.searchForm),
            //     getSexApi(this.searchForm),
            //     getDepartmentApi(this.searchForm),
            //     getIllAreaApi(this.searchForm),
            //     getDateDepartmentApi(this.searchForm)
            // ])
            return {
                getCount: () => getCountApi(this.searchForm),
                getKind: () => getKindApi(this.searchForm),
                getGroup: () => getGroupApi(this.searchForm),
                getSex: () => getSexApi(this.searchForm),
                getDepartment: () => getDepartmentApi(this.searchForm),
                getIllArea: () => getIllAreaApi(this.searchForm),
                getDateDepartment: () => getDateDepartmentApi({
                    ...this.searchForm,
                    startData: dayjs().add(-7, 'day').format('YYYY-MM-DD'),
                    endData: dayjs().format('YYYY-MM-DD')
                }),
                getLongTime: () => getLongTimeApi(this.searchForm)
            }
        },
        async getTemplate(id: number) {
            return await getTemplateApi({ id })
        },
        async getCount() {
            return await getCountApi(this.searchForm)
        },
        async getKind() {
            return await getKindApi(this.searchForm)
        },
        async getGroup() {
            return await getGroupApi(this.searchForm)
        },
        async getSex() {
            return await getSexApi(this.searchForm)
        },
        async getDepartment() {
            return await getDepartmentApi(this.searchForm)
        },
        async getIllArea() {
            return await getIllAreaApi(this.searchForm)
        },
        async getDateDepartment() {
            return await getDateDepartmentApi(this.searchForm)
        },
    }
})